<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class PaymentLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $where = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['p.bill_codes'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['p.bill_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $supplyer_code = $this->request->post('supplyer_code');
            if(!empty($supplyer_code)){
                $where['p.suppliers_code'] = $supplyer_code;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['p.suppliers_code|p.bill_codes'] = $keyword;
            }

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = Db::name('kuyou_payment_log')->alias('p')
                    ->join('kuyou_suppliers s','s.supplyer_code=p.suppliers_code')
                    ->where($where)
                    ->order(['p.bill_code'=>'ASC','p.id'=>'ASC'])
                    ->field('s.supplyer_name,FROM_UNIXTIME(p.bill_time,\'%Y-%m-%d\') as bill_time,p.suppliers_code,
                        p.business_type,p.bill_code,p.bill_codes,p.purchase_money,p.money_tax_price,p.discount_money,
                        p.real_money,p.pay_money,p.give_money,p.reamrk as remark')
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_payment_log')->alias('p')
                    ->join('kuyou_suppliers s','s.supplyer_code=p.suppliers_code')
                    ->where($where)
                    ->order(['p.bill_code'=>'ASC','p.id'=>'ASC'])
                    ->field('s.supplyer_name,FROM_UNIXTIME(p.bill_time,\'%Y-%m-%d\') as bill_time,p.suppliers_code,
                        p.business_type,p.bill_code,p.bill_codes,p.purchase_money,p.money_tax_price,p.discount_money,
                        p.real_money,p.pay_money,p.give_money,p.reamrk as remark')
                    ->select();
            }
            $row_count = Db::name('kuyou_payment_log')->alias('p')
                ->join('kuyou_suppliers s','s.supplyer_code=p.suppliers_code')
                ->where($where)->count();

            $price_decimal = Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');

            $oldcount = $count = [
                'money_tax_price'=>0,
                'purchase_money'=>0,
                'discount_money'=>0,
                'real_money'=>0,
                'pay_money'=>0,
                'give_money'=>0,
            ];
            $counts = $count;
            $counts['s_name'] = '合计';$counts['business_type'] = '-2';$counts['s_type'] = 3;
            $data = [];
            $field = 'bill_code';
            $now = isset($list[0][$field])?$list[0][$field]:'';
            $give_money = 0;
            $pay_money = 0;
            $status = true;
            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['payment_rate'] = '';
                $list[$list_k]['payment_rate'] = '';
                $list_v['s_type'] = 1;

                if($list_v[$field]!=$now){
                    $now = $list_v[$field];
                    $row = $count;
                    $row['s_name'] = '小计';$row['s_type'] = 2;$row['supplyer_name'] = '小计';$row['bill_time'] = '';$row['business_type'] = '-1';
                    if(($count['real_money'])>0){
                        $row['payment_rate'] = round($count['pay_money']/($count['real_money']),4)*100 .'%';
                    }else{
                        $row['payment_rate'] = '';
                    }
                    $data[] = $row;
                    foreach ($count as $count_k=>$count_v){
                        $counts[$count_k] += $row[$count_k];
                    }
                    $status = true;
                }

                if($list_v[$field]==$now){
                    if($status){
                        $give_money = $list_v['give_money'];$pay_money = $list_v['pay_money'];
                        $status = false;
                        $count = $oldcount;
                    }else{
                        $pay_money += $list_v['pay_money'];
                        $give_money += $list_v['give_money'];
                    }
                    $list_v['give_money'] = $give_money;
                    if(!$status){
                        foreach ($count as $count_k=>$count_v){
                            $count[$count_k] += $list_v[$count_k];
                        }
                        $count['give_money'] = $list_v['give_money'];
                    }
                }
                $data[] = $list_v;
                if($list_k==(sizeof($list)-1) || sizeof($list)==1){
                    $row = $count;$row['s_name'] = '小计';$row['s_type'] = 2;$row['supplyer_name'] = '小计';$row['bill_time'] = '';$row['business_type'] = '-1';
                    if($row['real_money']>0){
                        $row['payment_rate'] = round($count['pay_money']/($count['real_money']),4)*100 .'%';
                    }else{
                        $row['payment_rate'] = '';
                    }
                    $data[] = $row;
                    foreach ($count as $count_k=>$count_v){
                        $counts[$count_k] += $row[$count_k];
                    }
                }
            }

            if ($counts['real_money']>0){
                $counts['payment_rate'] = round($counts['pay_money']/($counts['real_money']),4)*100 .'%';
            }else{
                $counts['payment_rate'] = '';
            }
            $counts['supplyer_name'] = '合计';$counts['bill_time'] = '';$row['business_type'] = '-2';$counts['s_type'] = 3;
            $counts['purchase_money'] = round($counts['purchase_money'],$price_decimal);
            $counts['discount_money'] = round($counts['discount_money'],$price_decimal);
            $counts['real_money'] = round($counts['real_money'],$price_decimal);
            $counts['pay_money'] = round($counts['pay_money'],$price_decimal);
            $counts['give_money'] = round($counts['give_money'],$price_decimal);

            $result = array("count" => $row_count, "list" => $data,'sum'=>$counts);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
